package com.soft.sitegenerator.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.Sheet;
import com.soft.sitegenerator.business.hibernate.User;
import com.soft.sitegenerator.business.hibernate.WebSite;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.WebSiteEditForm;

public class WebSiteEditAction extends DispatchAction {

	public ActionForward editWebSite(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		WebSiteEditForm webSiteEditForm = (WebSiteEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		WebSite webSite = webSiteEditForm.getWebSite();
		webSite.setId(id);

		try {
			webSite.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Component component = new DataBaseComponent();
		User user = (User) component.getBean("usersHib");
		webSiteEditForm.setUsers(user.findAll());
		webSiteEditForm.setIdUser(webSite.getOwner().getId());

		Sheet sheet = (Sheet) component.getBean("sheetsHib");
		webSiteEditForm.setSheets(sheet.findAll());
		webSiteEditForm.setIdSheet(webSite.getSheet().getId());

		return mapping.findForward("showEdit");
	}

	public ActionForward deleteWebSite(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		WebSiteEditForm webSiteEditForm = (WebSiteEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		webSiteEditForm.getWebSite().setId(id);
		webSiteEditForm.getWebSite().delete();

		return mapping.findForward("showList");
	}

	public ActionForward addWebSite(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		WebSiteEditForm webSiteEditForm = (WebSiteEditForm) form;

		Component component = new DataBaseComponent();
		User user = (User) component.getBean("usersHib");
		webSiteEditForm.setUsers(user.findAll());

		Sheet sheet = (Sheet) component.getBean("sheetsHib");
		webSiteEditForm.setSheets(sheet.findAll());

		return mapping.findForward("showAdd");
	}

	public ActionForward saveWebSite(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		WebSiteEditForm webSiteEditForm = (WebSiteEditForm) form;

		Component component = new DataBaseComponent();
		User user = (User) component.getBean("usersHib");

		user.setId(webSiteEditForm.getIdUser());

		try {
			user.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Sheet sheet = (Sheet) component.getBean("sheetsHib");

		sheet.setId(webSiteEditForm.getIdSheet());

		try {
			sheet.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		WebSite webSite = webSiteEditForm.getWebSite();
		webSite.setOwner(user);
		webSite.setSheet(sheet);
		webSite.save();

		return mapping.findForward("showList");

	}

	public ActionForward updateWebSite(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		WebSiteEditForm webSiteEditForm = (WebSiteEditForm) form;

		Component component = new DataBaseComponent();
		User user = (User) component.getBean("usersHib");
		user.setId(webSiteEditForm.getIdUser());

		try {
			user.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Sheet sheet = (Sheet) component.getBean("sheetsHib");
		sheet.setId(webSiteEditForm.getIdSheet());

		try {
			sheet.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		WebSite webSite = webSiteEditForm.getWebSite();
		webSite.setOwner(user);
		webSite.setSheet(sheet);
		webSite.update();

		return mapping.findForward("showList");

	}

}